<template>
  <div class="h-panel-bar " align="center">
    <Button @click="confirm" color="primary" icon="h-icon-completed"
      :disabled="confirmFlag">确认</Button>&nbsp;&nbsp;&nbsp;&nbsp;
    <Button @click="close" color="green" icon="h-icon-error">返回</Button>&nbsp;&nbsp;&nbsp;&nbsp;
    <Button @click="reset" v-show="action == 'query'" icon="h-icon-refresh">重置</Button>
  </div>
</template>
<script>
export default {
  props: {
    id: {
      default: "id",
    },
  },
  data() {
    return {
      confirmFlag: false,
    };
  },
  computed: {
    action() {
      return this.$store.state.forms.action;
    },
  },
  methods: {
    confirm() {
      if (this.confirmFlag) {
        return;
      } else {
        this.confirmFlag = true;
        setTimeout(() => {
          this.confirmFlag = !this.confirmFlag;
        }, 2000);
      }
      if (this.$parent["validate"] instanceof Function) {
        if (this.$parent["validate"]() == false) {
          return;
        }
      }
      let validResult = this.$parent.$refs.form.valid();
      if (!validResult.result) {
        return;
      }
      if (this.action == "add") {
        this.add();
      } else if (this.action == "edit") {
        this.edit();
      } else if (this.action == "query") {
        this.query();
      }
    },
    query() {
      let data = this.$parent.model;
      this.$store.commit("queryData", JSON.parse(JSON.stringify(data)));
      this.$router.back();
    },
    add() {
      let data = this.$parent.model;
      this.$store.dispatch("addEvent", { _this: this, data: data });
    },
    edit() {
      let data = this.$parent.model;
      this.$store.dispatch("editEvent", { _this: this, data: data });
    },
    close() {
      this.$router.back();
    },
    reset() {
      this.$parent.model = {};
      this.$store.commit("queryData", {});
    },
  },
};
</script>